'
' Copyright (c) 2004-2005
' by Peter Donker ( info@bring2mind.net )
'
' Permission is hereby granted to use and/or modify this software for a single server. It is 
' NOT permitted to publish, distribute, sublicense, and/or sell copies of this software in its
' original or modified form. You are not allowed to remove this copyright notice.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
' BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
' NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
' DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
' OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OF THE SOFTWARE.
'
Imports System.IO
Imports System.Web.UI.WebControls

Namespace Bring2mind.DNN.Modules.Guestbook.Controls
 Public Class RoleListControl

  Inherits System.Web.UI.UserControl

  Private _ListBoxWidth As String = ""
  Private _ListBoxHeight As String = ""
  Private _Available As ArrayList
  Private _Assigned As String
  'Private _DataTextField As String = ""
  'Private _DataValueField As String = ""
  Private _Enabled As Boolean = True

  Protected WithEvents lstAvailable As System.Web.UI.WebControls.ListBox
  Protected WithEvents cmdAdd As System.Web.UI.WebControls.LinkButton
  Protected WithEvents cmdRemove As System.Web.UI.WebControls.LinkButton
  Protected WithEvents cmdAddAll As System.Web.UI.WebControls.LinkButton
  Protected WithEvents cmdRemoveAll As System.Web.UI.WebControls.LinkButton
  Protected WithEvents Label1 As System.Web.UI.WebControls.Label
  Protected WithEvents lstAssigned As System.Web.UI.WebControls.ListBox

  ' public properties
  Public Property ListBoxWidth() As String
   Get
    ListBoxWidth = Convert.ToString(ViewState(Me.ClientID & "_ListBoxWidth"))
   End Get
   Set(ByVal Value As String)
    _ListBoxWidth = Value
   End Set
  End Property

  Public Property ListBoxHeight() As String
   Get
    ListBoxHeight = Convert.ToString(ViewState(Me.ClientID & "_ListBoxHeight"))
   End Get
   Set(ByVal Value As String)
    _ListBoxHeight = Value
   End Set
  End Property

  Public WriteOnly Property Available() As ArrayList
   Set(ByVal Value As ArrayList)
    _Available = Value
   End Set
  End Property

  Public Property Assigned() As String
   Get
    Dim objListItem As ListItem
    Dim s As String
    For Each objListItem In lstAssigned.Items
     s &= objListItem.Value & ";"
    Next
    Return s
   End Get
   Set(ByVal Value As String)
    _Assigned = Value
   End Set
  End Property

  'Public WriteOnly Property DataTextField() As String
  ' Set(ByVal Value As String)
  '  _DataTextField = Value
  ' End Set
  'End Property

  'Public WriteOnly Property DataValueField() As String
  ' Set(ByVal Value As String)
  '  _DataValueField = Value
  ' End Set
  'End Property

  Public WriteOnly Property Enabled() As Boolean
   Set(ByVal Value As Boolean)
    _Enabled = Value
   End Set
  End Property

#Region " Web Form Designer Generated Code "


  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

  End Sub

  Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
   'CODEGEN: This method call is required by the Web Form Designer
   'Do not modify it using the code editor.
   InitializeComponent()
  End Sub

#End Region

  '*******************************************************
  '
  ' The Page_Load server event handler on this page is used
  ' to populate the role information for the page
  '
  '*******************************************************

  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
   Try
    ' Obtain PortalSettings from Current Context
    'Dim _portalSettings As PortalSettings = CType(HttpContext.Current.Items("PortalSettings"), PortalSettings)

    If Not Page.IsPostBack Then

     ' set dimensions of control
     If _ListBoxWidth <> "" Then
      lstAvailable.Width = System.Web.UI.WebControls.Unit.Parse(_ListBoxWidth)
      lstAssigned.Width = System.Web.UI.WebControls.Unit.Parse(_ListBoxWidth)
     End If
     If _ListBoxHeight <> "" Then
      lstAvailable.Height = System.Web.UI.WebControls.Unit.Parse(_ListBoxHeight)
      lstAssigned.Height = System.Web.UI.WebControls.Unit.Parse(_ListBoxHeight)
     End If

     ' set enabled
     lstAvailable.Enabled = _Enabled
     lstAssigned.Enabled = _Enabled

     ' save persistent values
     ViewState(Me.ClientID & "_ListBoxWidth") = _ListBoxWidth
     ViewState(Me.ClientID & "_ListBoxHeight") = _ListBoxHeight

    End If

   Catch exc As Exception 'Module failed to load
    'DotNetNuke.Services.Exceptions.ProcessModuleLoadException(Me, exc)
   End Try
  End Sub

  Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender
   If Not Me.IsPostBack Then
    Dim li As ListItem
    Dim StillAvailable As New ArrayList
    Dim Selected As New ArrayList
    For Each li In _Available
     If InStr(";" & _Assigned, ";" & li.Value & ";") = 0 Then
      StillAvailable.Add(li)
     Else
      Selected.Add(li)
     End If
    Next
    lstAvailable.DataTextField = "Text" '_DataTextField
    lstAvailable.DataValueField = "Value" '_DataValueField
    lstAvailable.DataSource = StillAvailable
    lstAvailable.DataBind()
    Sort(lstAvailable)

    lstAssigned.DataTextField = "Text" '_DataTextField
    lstAssigned.DataValueField = "Value" '_DataValueField
    lstAssigned.DataSource = Selected
    lstAssigned.DataBind()
    Sort(lstAssigned)
   End If
  End Sub

  Private Sub cmdAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdAdd.Click

   Dim objListItem As ListItem

   Dim objList As ArrayList = New ArrayList

   For Each objListItem In lstAvailable.Items
    objList.Add(objListItem)
   Next

   For Each objListItem In objList
    If objListItem.Selected Then
     lstAvailable.Items.Remove(objListItem)
     lstAssigned.Items.Add(objListItem)
    End If
   Next

   lstAvailable.ClearSelection()
   lstAssigned.ClearSelection()

   Sort(lstAssigned)

  End Sub

  Private Sub cmdRemove_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdRemove.Click

   Dim objListItem As ListItem

   Dim objList As ArrayList = New ArrayList

   For Each objListItem In lstAssigned.Items
    objList.Add(objListItem)
   Next

   For Each objListItem In objList
    If objListItem.Selected Then
     lstAssigned.Items.Remove(objListItem)
     lstAvailable.Items.Add(objListItem)
    End If
   Next

   lstAvailable.ClearSelection()
   lstAssigned.ClearSelection()

   Sort(lstAvailable)

  End Sub

  Private Sub cmdAddAll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdAddAll.Click

   Dim objListItem As ListItem

   For Each objListItem In lstAvailable.Items
    lstAssigned.Items.Add(objListItem)
   Next

   lstAvailable.Items.Clear()

   lstAvailable.ClearSelection()
   lstAssigned.ClearSelection()

   Sort(lstAssigned)

  End Sub

  Private Sub cmdRemoveAll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdRemoveAll.Click

   Dim objListItem As ListItem

   For Each objListItem In lstAssigned.Items
    lstAvailable.Items.Add(objListItem)
   Next

   lstAssigned.Items.Clear()

   lstAvailable.ClearSelection()
   lstAssigned.ClearSelection()

   Sort(lstAvailable)
  End Sub

  Private Sub Sort(ByVal ctlListBox As ListBox)

   Dim arrListItems As New ArrayList
   Dim objListItem As ListItem

   ' store listitems in temp arraylist
   For Each objListItem In ctlListBox.Items
    arrListItems.Add(objListItem)
   Next

   ' sort arraylist based on text value
   arrListItems.Sort(New ListItemComparer)

   ' clear control
   ctlListBox.Items.Clear()

   ' add listitems to control
   For Each objListItem In arrListItems
    ctlListBox.Items.Add(objListItem)
   Next

  End Sub

 End Class

 Public Class ListItemComparer
  Implements IComparer

  Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
   Dim a As ListItem = CType(x, ListItem)
   Dim b As ListItem = CType(y, ListItem)
   Dim c As New CaseInsensitiveComparer
   Return c.Compare(a.Text, b.Text)
  End Function
 End Class
End Namespace
